let SafeLoader = require("./SafeLoader");
let UIPropupBase = require("./UIPopupBase.js");
Global.UIPopupDisplayArea = cc.Enum({
    NONE: -1,
    MAIN: -1,
    GAME: -1
});
Global.UIPopupManager = {
    _loadingCountDownUI: null,
    _loadingUI: null,
    _popupList: [],
    _popupHideList: [],
    _beforPoupCallBack: [],
    _intervalId: -1,
    getPopupCompByUrl: function (e) {
        let t = this._getPopupByUrl(e);
        return t ? t.comp : null;
    },
    addPopupHideList: function (e, t, n) {
        for (let o = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : Global.UIPopupDisplayArea.NONE, i = arguments[4], a = 0; a < this._popupHideList.length; a++) {
            if (this._popupHideList[a].url === e) return;
        }
        let r = {
            error: null,
            url: e,
            res: null,
            afterCreate: t,
            displayAreaType: o,
            comp: null,
            isPersist: i
        };
        this._popupHideList.push(r);
    },
    _getPopupByUrl: function (e) {
        for (let t = this._popupList.length - 1; t >= 0; t--) {
            let n = this._popupList[t];
            if (n.url === e) {
                return n;
            }
        }
        return null;
    },
    _getPopupIndexByNode: function (e) {
        for (let t = this._popupList.length - 1; t >= 0; t--) {
            if (this._popupList[t].node === e) return t;
        }
        return -1;
    },
    addBeforPopupCallBack: function (e) {
        this._beforPoupCallBack.push(e);
    },
    _implementBeforPopupCallBack: function () {
        for (let e = void 0, t = 0; t < this._beforPoupCallBack.length; ++t) (e = this._beforPoupCallBack[t]) && e();
        this._beforPoupCallBack = [];
    },
    addPopup: function (e, t, n) {
        let self = this,
            a = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : Global.UIPopupDisplayArea.NONE,
            r = arguments[4];
        if (this._implementBeforPopupCallBack(), this._popupList) {
            for (let s = 0; s < this._popupList.length; ++s) {
                let l = this._popupList[s];
                if (e && l.url == e) {
                    return;
                }
            }
        }
        let c = {
            error: null,
            url: e,
            res: null,
            afterCreate: t,
            displayAreaType: a,
            comp: null,
            isPersist: r
        };
        if (this._popupList.push(c), !cc.js.isString(e)) return c.res = e, this._addPopup(c, n);
        let d = cc.loader.getRes(e, cc.Prefab);
        if (d && d._uuid.length > 0) {
            return c.res = d, this._addPopup(c, n);
        }
        (0, SafeLoader.safeLoadResWithType)(e, cc.Prefab, function (e, t) {
            if (!e) return c.error = e, c.res = t, self._addPopup(c, n);
            let o = self._getPopupIndexByNode(c.url);
            -1 !== o && self._popupList.splice(o, 1);
            let a = {
                text: "弹框加载失败，请稍后再试",
                confirmText: "确定",
                confirmSp: Global.spriteAtlas.getOKSpf()
            };
            Global.UIPopupManager.alert(a);
        });
    },
    _addPopup: function (e, t) {
        e.err;
        let n = e.res, o = e.afterCreate, a = this._getPopupByUrl(e.url);
        if (!a) {
            return null;
        }
        let node = cc.instantiate(n);
        a.node = node;
        let s = node.getComponent(UIPropupBase);
        s.init && s.init();
        e.comp = s;
        let rootCanvas = this._getRootCanvas(t, e);
        if (!rootCanvas) {
            let index = this._getPopupIndexByNode(node);
            -1 !== index && this._popupList.splice(index, 1);
        }
        return e.isPersist, rootCanvas && rootCanvas.addChild(node, 100), o && o(s), s.initHook && s.initHook(),
            s;
    },
    _getRootCanvas: function (e, t) {
        let n = cc.director.getScene();
        return e || (t.displayAreaType === Global.UIPopupDisplayArea.MAIN ? n.getChildByName("BaseCanvas") : t.displayAreaType === Global.UIPopupDisplayArea.GAME ? n.getChildByName("SceneRoot") : n.getChildByName("Canvas"));
    },
    node_onTouchStart: function (e) {
        e.stopPropagation();
    },
    removePopup: function (e) {
        if (e) {
            let t = this._getPopupIndexByNode(e);
            -1 !== t && this._popupList.splice(t, 1), e.destroy();
        }
    },
    removePopupInlist: function (e) {
        if (e) {
            let t = this._getPopupIndexByNode(e);
            -1 !== t && this._popupList.splice(t, 1);
        }
    },
    removePopupByUrl: function (e) {
        let t = this._getPopupByUrl(e);
        t && this.removePopup(t.node);
    },
    RemoveAll: function (e) {
        for (let t = this._popupList.length - 1; t >= 0; t--) {
            let n = this._popupList[t];
            n.isPersist && !e || this.removePopup(n.node);
        }
    },
    getPopupList: function () {
        return this._popupList;
    },
    removePopupInHideList: function (e) {
        if (e) for (let t = this._popupHideList.length - 1; t >= 0; t--) {
            let n = this._popupHideList[t];
            n.url === e && this._popupHideList.splice(n, 1);
        }
    },
    getPopupHideList: function () {
        return this._popupHideList;
    },
    hideLoadingCountDown: function () {
        this._loadingCountDownUI && (this._loadingCountDownUI.destroy(), this._loadingCountDownUI = null);
    },
    showLoadingCountDown: function (e, t, n) {
        if (null == this._loadingCountDownUI) {
            this._loadingCountDownUI = cc.instantiate(cc.loader.getRes("Dialog_TipsLoading")),
                this._loadingCountDownUI.on(cc.Node.EventType.TOUCH_START, this.node_onTouchStart, this._loadingCountDownUI);
            let o = cc.repeatForever(cc.rotateBy(1, 360));
            this._loadingCountDownUI.getChildByName("Panel_48657").getChildByName("Panel_middle").getChildByName("ImageView_juhua").runAction(o),
                cc.game.addPersistRootNode(this._loadingCountDownUI), this._loadingCountDownUI.getComponent("LoadingCountDown").startTime(e, t, n);
        } else this._loadingCountDownUI.active = !0;
    },
    showLoading: function (e) {
        if (!this._loadingUI) {
            let t = cc.find("TipsManager");
            if (!t) return;
            let n = Global.utils.searchNode(t, "LoadTips"),
                o = Global.utils.searchComp(n, "Lbl_Text", cc.Label);
            if (!n || !o) return;
            n.on("touchstart", this.node_onTouchStart, n), this._loadingUI = {
                node: n,
                text: o
            };
        }
        this._loadingUI.node && (this._loadingUI.node.active = !0, this._loadingUI.text.string = e);
    },
    hideLoading: function () {
        this._loadingUI && (this._loadingUI.node.active = !1);
    },
    alert: function (e) {
        let t = e.parent;
        this.addPopup("PopupPanel/PopupAlert", function (t) {
            return t.initAlert(e.text, e.confirmSp, e.cancelSp, e.confirmText, e.cancelText, e.confirmCallback, e.cancelCallback, e.closeCallback, e.needCloseBtn, e.needConfirmBtn),
                t;
        }, t, null, e.isPersist);
    },
    alertLarge: function (e) {
        let t = e.parent;
        this.addPopup("Common/PopupAlertL", function (t) {
            return t.initAlert(e.text, e.confirmText, e.cancelText, e.confirmCallback, e.cancelCallback),
                t;
        }, t);
    }
}
